package game.model.animation{
	import flash.geom.Point;
	
	import game.data.constant.name.CoreName;
	import game.data.constant.name.NotificationName;
	import game.data.constant.name.RoleName;
	import game.data.constant.system.Path;
	import game.data.structure.animation.Animation;
	import game.data.structure.animation.AnimationData;
	import game.data.structure.animation.FrameData;
	
	import lib.gameEngine.common.data.Notification;
	import lib.gameEngine.core.Model;
	import lib.utile.loader.BulkLoader;
	
	public class AnimationModel extends Model{
		
		private var _loader:BulkLoader;
		private var _animationPool:Object;
		private var _resClass:Class;   //图片资源类
		
		/**
		 * 动画模型
		 * 
		 * 本类中存储了各种角色的动画资源，以RoleName中的名称作为索引
		 * 
		 * TODO 目前存储动画的格式过于复杂
		 * 
		 *  
		 * 
		 */		
		public function AnimationModel(){
			super(CoreName.ANIMATION);
			_loader = BulkLoader.getLoader(CoreName.LOAD);
			_animationPool = new Object();
		}
		

		/**
		 * 
		 * 获取动画池
		 *  
		 * @return 
		 * 
		 */		
		public function get animationPool():Object{
			return _animationPool;
		}
		
		/**
		 * TODO  本类用于测试，待完善
		 * 
		 * 返回的是引用的图片资源类
		 * 
		 * 
		 *  
		 * @return 
		 * 
		 */		
		public function get resClass():Class{
			return _resClass;
		}

		/**
		 * 解析XML
		 * 
		 * 整个动画的存储结构如下：
		 * 
		 * animationPool
		 * +角色名称 animation
		 * 	+行为名称 animationData
		 *   +方向名称 frames
		 * 	  +帧号 frameData
		 * 
		 * 
		 * 怎么样，是不是很复杂，帮我想想办法吧。
		 * 
		 * 具体的我也不解释了，看代码吧
		 * 
		 *  
		 * 
		 */		
		public function parseXML():void{
			var archer:XML = _loader.getXML(Path.XML_PATH,true);
			var animation:Animation = new Animation();
			for each(var xml:XML in archer.action){
				var animationData:AnimationData = new AnimationData();
				animationData.frames = new Object();
				for each(var frames:XML in xml.frames){
					animationData.fps = frames.@fps;
					animationData.direction = frames.@direction;
					if(!animationData.frames[animationData.direction])
					animationData.frames[animationData.direction] = new Array();
					for each(var xmlData:XML in frames.frame){
						var frameData:FrameData = new FrameData();
						frameData.index = xmlData.@no;
						frameData.point = new Point(xmlData.@x,xmlData.@y);
						frameData.offsetPoint = new Point(xmlData.@ox,xmlData.@oy);
						frameData.width = xmlData.@w;
						frameData.height = xmlData.@h;
						animationData.frames[animationData.direction].push(frameData);
					}
					animation.addAnimationData(xml.@name,animationData);
				}
			}
			_animationPool[RoleName.ROLE] = animation;
		}
		
		public function saveBm():void{
			var resClass:Class = _loader.get(Path.SWF_PATH).getDefinition("Archer");
			_loader.remove(Path.SWF_PATH);  
			_resClass = resClass;
			this.sendNotify(new Notification(NotificationName.TEST));
		}
	}
}